import java.awt.Graphics;
import java.awt.Image;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Content extends JPanel {

	public boolean is_pause = false;
	public boolean is_start = true;
	public boolean is_end   = false;

	private Grid grid;
	private int score = 0;
	public int level = 1;
	private int nb_lines = 0;

	private Image background;
	private Image red;
	private Image yellow;
	private Image black;
	private Image cyan;
	private Image green;
	private Image pink;
	private Image blue;

	private Image pause;
	private Image start;
	private Image end;

	private Font font;

	public Content(Grid g)
	{
		super();
		this.grid = g;

		try{
			background = ImageIO.read(new File("../img/tetris.bmp"));
			red        = ImageIO.read(new File("../img/rouge.BMP"));
			yellow     = ImageIO.read(new File("../img/jaune.BMP"));
			black      = ImageIO.read(new File("../img/noir.BMP"));
			cyan       = ImageIO.read(new File("../img/cyan.BMP"));
			green      = ImageIO.read(new File("../img/vert.BMP"));
			pink       = ImageIO.read(new File("../img/rose.BMP"));
			blue       = ImageIO.read(new File("../img/bleu.BMP"));
			start      = ImageIO.read(new File("../img/start.png"));
			end        = ImageIO.read(new File("../img/end.png"));
			pause      = ImageIO.read(new File("../img/pause.png"));

		}
		catch (IOException e) {
			e.printStackTrace();
		}

		this.font = new Font("Courrier",Font.BOLD,36);

	}

	public void reinit()
	{
		score= 0;
		level = 1;
		nb_lines = 0;
	}

	private void drawColorCube(int color, int x, int y, Graphics g)
	{
		switch (color)
		{
			case 1:
				g.drawImage(red,x,y,this);
				break;
			case 2:
				g.drawImage(yellow,x,y,this);
				break;
			case 3:
				g.drawImage(black,x,y,this);
				break;
			case 4:
				g.drawImage(cyan,x,y,this);
				break;
			case 5:
				g.drawImage(green,x,y,this);
				break;
			case 6:
				g.drawImage(pink,x,y,this);
				break;
			case 7:
				g.drawImage(blue,x,y,this);
				break;
			default:
				break;
		}
	}

	private void drawInfos(Graphics g)
	{
		g.drawString("LEVEL",310,390);
		g.drawString(String.valueOf(level),310,435);
		g.drawString("SCORE",310,265);
		g.drawString(String.valueOf(score),310,310); 
	}

	public void paintComponent(Graphics g)
	{
		// Fond
		g.drawImage(background,0,0,this);

		// Tas
		for (int y = 4; y < 20;y++)
			for(int x= 0; x < 9;x++)
				drawColorCube(this.grid.grid[y][x],x*32,y*32,g);

		// Pièce courante
		for (int y = 0; y < grid.sizeActiveShape(); y++)
			for (int x = 0; x < grid.sizeActiveShape();x++)
				drawColorCube(this.grid.getActiveShape()[y][x],(x+this.grid.getActiveShapeX())*32, (y+grid.getActiveShapeY())*32,g);

		// Pièce suivante
		for (int y = 0; y < grid.sizePendingShape();y++)
			for (int x = 0; x < grid.sizePendingShape(); x++)
				drawColorCube(this.grid.getPendingShape()[y][x],x*32 + 320,y*32 + 32,g);

		// Score
		g.setFont(this.font);
		if (is_end)
		{
			g.drawImage(end,0,0,this);
			g.drawString(String.valueOf(score),215,280); 
			g.drawString(String.valueOf(level),223,335);
		}

		// Début de partie
		else if (is_start)
			g.drawImage(start,0,0,this);

		// Pause
		else if (is_pause)
		{
			g.drawImage(pause,0,0,this);
			drawInfos(g);
		}


		else
		{
			drawInfos(g);
		}
	}

	public void rotate()
	{
		grid.rotate();
		this.repaint();
	}

	public void setGrid(Grid g)
	{
		this.grid = g;
		this.is_start = true;
		this.is_end = false;
		this.is_pause = false;
		this.repaint();
	}

	public void moveDown()
	{

		int nb_shrink = grid.moveDown();
		nb_lines += nb_shrink;
		if (nb_lines > 20)
		{
			level +=1;
			nb_lines = nb_lines %15;
		}

		switch (nb_shrink)
		{
			case 1:
				score += 10;
				break;
			case 2:
				score += 30;
				break;
			case 3:
				score += 60;
				break;
			case 4:
				score += 120;
				break;
		}

		for (int i = 0; i < grid.grid[4].length;i++)
			if (grid.grid[4][i] != 0)
				is_end = true;

		this.repaint();
	}

	public void moveLeft()
	{
		grid.moveLeft();
		this.repaint();

	}

	public void moveRight()
	{
		grid.moveRight();
		this.repaint();

	}


	public void setScore(int score)
	{
		this.score = score;
	}

	public int getScore()
	{
		return this.score;
	}




}
